<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/12/24 0024
  Time: 14:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
	<meta charset="UTF-8">
    <title>生产工序设计单</title>
    <link rel="stylesheet"  href="/bootstrap/bootstrap-5.3.3-dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="/css/ProductDesign/module(1).css" />
    <style type="text/css">
    	
    </style>
</head>
<body style="padding: 15px 50px;background-color: #eeeeee;">
	<div class="panel-heading hhead">
		<nav aria-label="breadcrumb">
		  <ol class="breadcrumb">
		  	<span style="color: blue;">您正在做的业务是：</span>
		    <li class="breadcrumb-item"><a href="#">生产管理</a></li>
		    <li class="breadcrumb-item"><a href="#">产品生产工序设计</a></li>
		    <li class="breadcrumb-item active" aria-current="page">制定产品生产工序设计单</li>
		  </ol>
		</nav>
	</div>
    <div style="margin-bottom: 2px">
        <div style="display: inline;margin-left: 66%;">
            <button class="btn btn-primary" id="Add">添加工序</button>
            <button class="btn btn-primary" id="delete">删除工序</button>
            <button class="btn btn-primary" id="submit">提交</button>
<%--            <button class="btn btn-primary">返回</button>--%>
        </div>
    </div> <!-- 此div改用栅格来做 -->
    <div id="designListBox">
        <div style="text-align: center">
            <h3>生产工序设计单</h3>
        </div>
        <div style="padding: 30px">
           <table class="tbl1">
           	<tr>
                   <td class="col1">产品名称：</td>
                   <td colspan="1" id="productName"></td>
                   <td class="col1">产品编号：</td>
                   <td colspan="2" id="productId">

                   </td>
               </tr>
               <tr>

                   <td class="col1">设计人：</td>
                   <td class="col2">
                   	<input type="text" class="txt_under_line" id="Designer">
                   </td>
               </tr>
               
           </table>

            <table class="table table-bordered" style="margin: 15px auto;">
                <thead>
                <tr>
                    <th>点选</th><th>工序名称</th><th>工序编号</th>
                    <th style="width: 250px;">描述</th>
                    <th style="width: 65px;">工时数</th>
                    <th style="width: 81px;">工时单位</th>
                    <th style="width: 113px;">单位工时成本</th>
                    <th>工时成本小计（元）</th>
                </tr>
                </thead>
                <tbody id="template">

                </tbody>

            </table>

            <table class="tbl3">
            	<tr>
                    <td class="col1">登记人：</td>
                    <td class="col2"><input type="text" class="txt_under_line" id="Registrants"></td>
                    <td class="col1">登记时间：</td> <td class="col2" id="time">系统时间</td>
               </tr>
                <tr>
                    <td class="col1">设计要求：</td>
                    <td colspan="3" style="padding-top: 5px">
                        <textarea rows="4" cols="100" id="moduleDescribe"></textarea>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="modal fade" id="AddProductProcessModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog modal-xl">
            <div class="modal-content" style="height: 450px">
                <div class="modal-header" style="height: 8%">
                    <h3 class="modal-title" id="staticBackdropLabel">添加工序</h3>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" style="height: 100%;width: 100%;margin: -1%">
                    <iframe id="iframe2" src="" style="width: 100%;height: 99%"></iframe>
                </div>
                <div class="modal-footer" style="height: 15%">
                    <%--                <button id="btnOK" class="btn btn-success">--%>
                    <%--                    确定<span class="glyphicon glyphicon-ok"></span>--%>
                    <%--                </button>--%>

                    <button id="btnConcel" class="btn btn-success" data-bs-dismiss="modal">
                        确认<span class="glyphicon glyphicon-remove"></span>
                    </button>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="/js/jquery-3.7.0.min.js"></script>
    <script type="text/javascript" src="/bootstrap/bootstrap-5.3.3-dist/js/bootstrap.bundle.min.js"></script>
    <script type="text/javascript" src="/js/common.js"></script>
    <script type="text/javascript">
        $(function (){
            initialProcess();
            // 初始化时间
            setInterval(updateDateTime, 1000);

            $("#Add").click(function () {
                $("#AddProductProcessModal").modal("show");
                $("#iframe2").attr("src", "/m-design-procedure/AddProductionProcessItems");
            })
            $("#delete").click(function () {
                let rows = $("#template tr");
                console.log(rows)
                let flag = false;
                for (let i = 0; i < rows.length; i++) {
                    let row = rows[i];
                    let checkbox = $(row).find("input[type='checkbox']");
                    if (checkbox.prop("checked")) {
                        $(row).remove();
                        flag = true;
                    }
                }
                if (!flag) {
                    alert("请选择要删除的行");
                }
            })
            $("#submit").click(function () {
                let designer = $("#Designer").val();
                if (designer == "") {
                    alert("请输入设计人");
                    return; // 弹出提示框
                }
                if($("#Registrants").text() == ""){
                    $("#Registrants").val(designer);
                }
                if($("#template tr").length==0){
                    alert("请添加工序(至少一种)");
                    return;
                }
                let register = $("#Registrants").text();
                let productId = $("#productId").text();
                let moduleDescribe = $("#moduleDescribe").text()

                let rows = $("#template tr");
                let data = [];
                data.push({
                    productId: productId,
                    designer: designer,
                    register: register,
                    moduleDescribe: moduleDescribe
                })
                for (let i = 0; i < rows.length; i++) {
                    let row = rows[i];
                    let procedureId = $(row).find("td:eq(2)").text();
                    let procedureName = $(row).find("td:eq(1)").text();
                    let procedureDescribe = $(row).find("td:eq(3)").text();
                    let labourHourAmount = $(row).find(".num-input:eq(0)").val();
                    if(labourHourAmount == ""){
                        alert("请输入"+"'"+procedureName+"'工序的工时数");
                        return;
                    }
                    let amountUnit = $(row).find(".num-input:eq(1)").val();
                    if(amountUnit == ""){
                        alert("请输入"+"'"+procedureName+"'工序的工时单位");
                        return;
                    }
                    let costPrice = $(row).find(".num-input:eq(2)").val();
                    if(costPrice == ""){
                        alert("请输入"+"'"+procedureName+"'工序的单位工时成本");
                        return;
                    }
                    let subtotal = $(row).find("td:eq(7)").text();
                    let obj = {
                        procedureId: procedureId,
                        procedureName: procedureName,
                        procedureDescribe: procedureDescribe,
                        labourHourAmount: labourHourAmount,
                        amountUnit: amountUnit,
                        costPrice: costPrice,
                        subtotal: subtotal
                    }
                    data.push(obj);
                }
                console.log(data)
                sessionStorage.setItem("data", JSON.stringify(data));
                alert("提交成功");
            })
            $("#AddProductProcessModal").on("hidden.bs.modal", function () {
                $("#iframe2").attr("src", "");
                let IdItems = sessionStorage.getItem("IdItems");
                console.log(IdItems)
                // sessionStorage.clear();
                IdItems = JSON.parse(IdItems);
                if (IdItems == null){
                    return;
                }
                $("#template").empty();
                for (let i = 0; i < IdItems.length; i++) {
                    let id = IdItems[i];
                    addToTable(id);
                }
            })
            $("#template").on("input",".num-input", function () {
                let row = $(this).parent().parent();
                let num = $(row).find(".num-input:eq(0)").val();
                let costPrice = $(row).find(".num-input:eq(2)").val();
                let total = costPrice * num;
                $(row).find("td:eq(7)").text(total);
            })

        })
        function initialProcess(){
            let id = sessionStorage.getItem("id");
            sessionStorage.clear();
            $.ajax({
                url: "/d-file/initialMaterial?id=" + id,
                type: "get",
                datatype: "json",
                success: function (data) {
                    console.log(data)
                    $('#productName').text(data.productName);
                    $('#productId').text(data.productId)
                }
            })
        }
        function addToTable(id){
            if (id == null) {
                return;
            }
            $.ajax({
                url: "/m-procedure-unit/getProcessById?id=" + id,
                type: "get",
                datatype: "json",
                success: function (data) {
                    console.log(data)
                    let tr = "<tr>" +
                        "<td class='ckbox'><input type='checkbox' /></td>" +
                        "<td>" + data.procedureName + "</td>" +
                        "<td>" + data.procedureId + "</td>" +
                        "<td>" + data.procedureDescribe + "</td>"+
                        "<td class='num'><input class='num-input'></td>" +
                        "<td class='num'><input class='num-input'></td>" +
                        "<td class='num'><input class='num-input'></td>" +
                        "<td></td>" +
                        "</tr>";
                    $("#template").append(tr);
                }
            })
        }
        function updateDateTime(){
            let date = new Date();
            let year = date.getFullYear();
            let month = (date.getMonth() + 1).toString().padStart(2, '0');
            let day = date.getDate().toString().padStart(2,'0');
            let hours = date.getHours().toString().padStart(2,'0');
            let minutes = date.getMinutes().toString().padStart(2,'0');
            let seconds = date.getSeconds().toString().padStart(2,'0');
            let dateTimeStr = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
            $("#time").text(dateTimeStr);
        }
    </script>
</body>
</html>
